package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Admin;
import com.example.entity.Experiment;
import com.example.service.AdminService;
import com.example.service.ExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("experiments")
public class ExperimentController {

    @Autowired
    private ExperimentService experimentService;
    @Autowired
    private AdminService adminService;


    /**
     * 分页查询所有工作量实验室教学信息（二级管理员查询所有）
     * @return
     */
    @GetMapping("/getPageExperiment/{currentPage}/{pageSize}")
    public R getPageExperiment(@PathVariable int currentPage,@PathVariable int pageSize){


        IPage page =new Page(currentPage,pageSize);

        IPage page1 = experimentService.page(page);

       return R.success("分页查询所有工作量实验室教学信息",page1);

    }

    /**
     * 分页查询当前工号实验室教学工作量信息（教职工查询自己的）
     * @return
     */

    @GetMapping("/getPageExperiment/{currentPage}/{pageSize}/{jobNumber}")

    public R getPageByJobNumber(@PathVariable int currentPage,@PathVariable int pageSize,@PathVariable Integer jobNumber){

        LambdaQueryWrapper<Experiment> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Experiment::getJobNumber,jobNumber);

        IPage page =new Page(currentPage,pageSize);
        IPage page1 = experimentService.page(page,lqw);

        return R.success("分页查询当前工号实验室教学工作量信息",page1);
    }

    /**
     * 通过专业分页查询（一级管理员查看所有自己专业的）
     * @return
     */

    @GetMapping("/getByMajorAndAdminJobNumber/{currentPage}/{pageSize}/{jobNumber}")

    public R getByMajorAndAdminJobNumber(@PathVariable int currentPage, @PathVariable int pageSize, @PathVariable int jobNumber){

        //获取当前管理员信息
        LambdaQueryWrapper<Admin> lqwAdmin= new LambdaQueryWrapper<>();
        lqwAdmin.eq(Admin::getJobNumber,jobNumber);
        Admin one = adminService.getOne(lqwAdmin);


        //根据管理员的专业获得对应专业的实验室教学工作量
        LambdaQueryWrapper<Experiment> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Experiment::getMajor,one.getMajor());

        IPage page =new Page(currentPage,pageSize);
        IPage page1 = experimentService.page(page,lqw);

        return R.success("通过专业分页查询（一级管理员查看所有自己专业的实验室教学工作量）",page1);
    }

    /**
     * 添加实验室教学工作量
     * @param experiment
     * @return
     */
    @PostMapping("/addExperiment")
    public R add(@RequestBody Experiment experiment){

        experimentService.save(experiment);
        return R.success("添加实验室教学工作量",experiment);
    }

    /**
     * 通过id删除实验室教学工作量
     * @param id
     * @return
     */
    @DeleteMapping("/deleteExperiment/{id}")
    public R deleteById(@PathVariable int id){

        boolean b = experimentService.removeById(id);
        if (b){
            return R.success("通过id删除实验室教学工作量成功！",null);
        }else{
            return R.error("通过id删除实验室教学工作量失败！");
        }
    }

    /**
     * 教职工修改个人实验室教学工作量
     * @param experiment
     * @return
     */
    @PutMapping("/updateExperiment")
    public R updateExperiment(@RequestBody Experiment experiment){

        boolean b = experimentService.updateById(experiment);
        if (b){
            return R.success("教职工修改个人实验室教学工作量成功",experiment);
        }else {
            return R.error("教职工修改个人实验室教学工作量失败");
        }
    }

    /**
     * 修改审核状态
     * @return
     */
    @PutMapping("/updateStatus")
    public R updateStudentStatus(@RequestBody Experiment experiment){
        boolean updateById = experimentService.updateById(experiment);
        if (updateById){
            return R.success("修改审核状态成功",experiment);
        }else {
            return R.error("修改审核状态失败！");
        }
    }

}

